// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';
import router from './router';
import store from './vuex/store'
Vue.use(ElementUI);



Vue.config.productionTip = false

// // 导航守卫
router.beforeEach((to, from, next) => {

    // 第一步 先去vuex拉取用户信息
    let info = store.state.userInfo
    let infoLength = Object.keys(info).length === 0
        // infoLength为true 说明是第一次访问系统或者刷新页面,在这里判断路由'/login'，防止进入死循环。未登录，status为1，跳到登录页面，已登录，status为0，继续访问页面
    if (infoLength) {
        store.dispatch('getUserInfo').then((res) => {
            if (res.data.status === '0') {
                store.dispatch('userLogin', res.data.data) // 将用户基本信息存入vuex
                store.dispatch('permission', res.data.data.userType).then(() => { // 根据用户角色，动态添加权限路由
                    router.addRoutes(store.state.navList)
                    next({...to, replace: true })
                })
            } else if (res.data.status === '1') {
                next('/login');
            }
        })
    } else {
        next();
    }


});
/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>'
})